/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.broker.consumer.controller.base;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import me.send.broker.entity.Message;
import me.send.broker.enums.AuthodType;
import me.send.broker.service.BrokerEvaluateService;
import me.send.broker.service.BrokerService;
import me.send.broker.service.ComplainService;
import me.send.broker.service.MessageService;
import me.send.broker.service.OpenCityService;
import me.send.broker.service.OrderProgressService;
import me.send.broker.service.OrderService;
import me.send.broker.service.PhoneNumberService;
import me.send.broker.service.SysconfigService;
import me.send.broker.service.UserEvaluateService;
import me.send.broker.service.UserService;
import me.send.broker.service.VersionService;
import me.send.broker.service.WalletService;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;

import com.broker.consumer.controller.helper.MailSendHelper;

/**
 * @author maxmin
 *
 * @date 2015年8月3日 下午5:50:58
 */
public class BaseController {
	
	@Autowired
	protected BrokerService brokerService;
	
	@Autowired
	protected PhoneNumberService phoneNumberService;
	
	@Autowired
	protected OrderService orderService;
	
	@Autowired
	protected WalletService walletService;
	
	@Autowired
	protected MailSendHelper sendMaileUtil;
	
	@Autowired
	protected ComplainService complainService;
	
	@Autowired
	protected MessageService messageService;
	
	@Autowired
	protected BrokerEvaluateService brokerEvaluateService;
	
	@Autowired
	protected UserEvaluateService userEvaluateService;
	
	@Autowired
	protected UserService userService;
	
	@Autowired
	protected OpenCityService openCityService;
	
	@Autowired
	protected SysconfigService sysconfigService;
	
	@Autowired
	protected VersionService versionService;
	
	@Autowired
	protected OrderProgressService orderProgressService;
	
	/**
	 * 保存异常信息
	 * 
	 * @param authorName
	 * @param modelName
	 * @param functionName
	 * @param e
	 */
	protected void saveException(String authorName, String modelName,
			String functionName, Exception e) {
		
		try {
			// Exception的具体说明
			ByteArrayOutputStream baos = new ByteArrayOutputStream();  
	        e.printStackTrace(new PrintStream(baos));  
	        String exception = baos.toString();  
			
	        // 获得当前的IP地址
			InetAddress addr = InetAddress.getLocalHost();
			String ip=addr.getHostAddress().toString();//获得本机IP
			
			// 发送错误的邮件
			sendMaileUtil.sendHtmlMail("tjl3323@126.com", "["+ip+"]"+authorName+":"+modelName+"==>"+functionName, exception);
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param userId 用户或者经纪人id
	 * @param message 消息内容
	 * @param userType 
	 */
	protected void saveMessage(Message message) {
		try {
			message.setCreatetime(HelperUtil.getCurrentDate());
			message.setUpdatetime(HelperUtil.getCurrentDate());
			messageService.addMessage(message);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(), "BaseController", "saveMessage()", e);
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取客户端ip
	 * @param request
	 * @return
	 * @author maxmin
	 * @date 2015年9月18日上午10:43:44
	 */
	protected String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    return ip;  
	}  
}
